import { http } from '@/utils/http'
import { taskTypes } from '@/config/dict'

export const useHomeStore = () => {
  const taskCode = 'all'
  const taskType = '_reservation_order'

  let timeoutId
  let ismounted = false

  const task = reactive({
    taskCode,
    taskType,
    loading: false,
    list: [],
    expand: null,

    page: 1,
    pageSize: 20,
    total: 0,
  })

  const board = reactive({
    taskCode,
    taskType,
    list: [],
    taskNames: [],

    page: 1,
    pageSize: 20,
    total: 0,
  })

  // 获取任务数据
  const getTasks = (loading = true) => {
    clearInterval(task.intervalId)
    if (loading) task.loading = true
    if (ismounted) {
      http
        .get('/order/order_task_data_list', {
          params: {
            page: task.page,
            page_size: task.pageSize,
            task_type: task.taskType,
          },
        })
        .then(res => {
          task.list = res.data.list
          task.total = res.data.total

          clearInterval(task.intervalId)
          task.intervalId = setInterval(() => getTasks(false), 5000)
        })
        .finally(() => {
          task.loading = false
        })
    }

  }

  // 选中任务
  const selectTask = item => {
    task.expand = null

    if (task.taskCode == item.task_code) {
      task.taskCode = taskCode
      board.taskCode = taskCode
    } else {
      task.taskCode = item.task_code
      board.taskCode = task.taskCode
      getBoardDetail()
    }
  }

  // 展开
  const expandTask = item => {
    // 收起
    if (task.expand == item.task_code) {
      task.expand = null
      return
    }
    task.taskCode = item.task_code
    task.expand = item.task_code
    board.taskCode = item.task_code
    getBoardDetail()
  }

  // 切换任务类型
  const switchTaskType = () => {
    getTasks()
    board.taskType = task.taskType
    board.taskCode = taskCode
    board.list = []
    board.total = 0
    // getBoardList()
    getBoardTaskNames()
  }

  // 获取看板列表数据
  const getBoardList = () => {
    board.loading = true
    board.list = []

    http
      .get('/order/order_task_data_statistics', {
        params: {
          page: board.page,
          page_size: board.pageSize,
          task_code: board.taskCode,
          task_type: board.taskType,
        },
      })
      .then(res => {
        board.list = res.data
        board.total = res.total
        board.list = formatBoardData(res.data.list)
      })
      .finally(() => {
        board.loading = false
      })
  }

  // 获取看板详情
  const getBoardDetail = () => {
    board.loading = true
    http
      .get('/order/order_task_data_statistics_one', {
        params: {
          page: 1,
          page_size: 300,
          task_code: board.taskCode,
          task_type: board.taskType,
        },
      })
      .then(res => {
        board.list = formatBoardData(res.data)
      })
      .finally(() => {
        board.loading = false
      })
  }

  // 看板搜索
  const searchBoard = () => {
    getBoardList()
  }

  // 获取
  const getBoardTaskNames = () => {
    http
      .get('/order/get_order_task_name_list', {
        params: {
          task_type: board.taskType,
        },
      })
      .then(res => {
        board.taskNames = res.data.map(item => {
          return {
            ...item,
            label: item.task_name,
            value: item.task_code,
          }
        })
      })
  }

  // 格式化看板数据
  function formatBoardData(data) {
    console.log(data);

    return data.map(item => {
      const { task_type } = item

      if (!task_type) return item

      const taskType = taskTypes.find(item => item.value === task_type)
      item._taskTypeDesc = taskType.label

      // 成功率
      item._successRate = item.order_total_count
        ? ((item.order_success_count / item.order_total_count) * 100).toFixed(0)
        : 0

      // 完成率
      item._completeRate = item.total_stake ? ((item.success_stake / item.total_stake) * 100).toFixed(0) : 0

      for (const key in item?.gun_data) {
        const gurnItem = item?.gun_data[key]

        // 进度
        gurnItem._percent = gurnItem.total_stake
          ? Number(((gurnItem.success_stake / gurnItem.total_stake) * 100).toFixed(0))
          : 0
      }

      return item
    })
  }

  onMounted(() => {
    ismounted = true
    getTasks()
    // getBoardList()
    getBoardTaskNames()

    if (localStorage.isDev) return

  })

  onUnmounted(() => {
    ismounted = false
    clearInterval(task.intervalId)
  })

  return {
    task,
    board,

    getTasks,
    selectTask,
    switchTaskType,
    expandTask,

    getBoardList,
    getBoardDetail,
    searchBoard,
    getBoardTaskNames,
  }
}
